/* 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

goog.provide('kramerius.audio.Controller');

goog.require('kramerius.audio.ui.VolumeInfo');

/** @const */ var MIN_VOLUME = 0;
/** @const */ var MAX_VOLUME = 100;
/** @const */ var DEFAULT_VOLUME = 50;

/**
 * @constructor 
 * @param {boolean} loggingEnabled whether logging should be enabled (for debugging)
 */
kramerius.audio.Controller = function(loggingEnabled){
    /** @private **/
    this.loggingEnabled_ = loggingEnabled;
    /** @private **/
    this.audioPlayer_ = undefined;
    /** @private **/
    this.playList_ = undefined;
    /** @private **/
    this.volume_ = DEFAULT_VOLUME;
    /** @private **/
    this.mute_ = false;
    /** @private **/
    this.volumeInfo_ = undefined;
    /** @private **/
    this.rotatingRecordAnimation_ = undefined;
    this.playNextButton_ = undefined;
    this.playPreviousButton_ = undefined;
    this.log_("initializing" );
};


kramerius.audio.Controller.prototype.setPosition = function(position){
    this.audioPlayer_.setPosition(position);
    if (this.progressBar_ != undefined){
        this.progressBar_.setPosition(position);
    }
    if (this.progressInfo_ != undefined){
        this.progressInfo_.setPosition(position/1000);
    }
}


kramerius.audio.Controller.prototype.setTotalLength = function(length){
    if (this.progressBar_ != undefined){
        this.progressBar_.setTotalLength(length);
    }
    if (this.progressInfo_ != undefined){
        this.progressInfo_.setTotalLength(length/1000);
    }
};


kramerius.audio.Controller.prototype.setAudioPlayer = function(audioPlayer){
    this.audioPlayer_ = audioPlayer;
};


kramerius.audio.Controller.prototype.setTrack = function(track, playNow){
    this.audioPlayer_.stop();
    this.log_("setting track " + track);
    if (this.playingTrackInfo_!= undefined){
        if (track!=null){
            this.playingTrackInfo_.setText(track.getTitle());    
        }else{
            this.playingTrackInfo_.resetEmpty();    
        }
    }
    this.audioPlayer_.setTrack(track,playNow);
};

//tohle se spise logicky vaze k playlistu
kramerius.audio.Controller.prototype.refreshPlayNextPlayPreviousButtons = function(){
    //PLAY NEXT button
    if (this.canPlayNextTrack()){
        this.playNextButton_.enable();
    }else{
        this.playNextButton_.disable();
    }
    //PLAY PREVIOUS button
    if (this.canPlayPreviousTrack()){
        this.playPreviousButton_.enable();
    }else{
        this.playPreviousButton_.disable();
    }
}

kramerius.audio.Controller.prototype.canPlayNextTrack = function(){
    if (this.playlist_ == null){
        this.log_("canPlayNextTrack: playlist is null");
    }
    return this.playlist_.getNextItemToPlay() != null;
}

kramerius.audio.Controller.prototype.playNextTrack = function(){
    var track = this.playlist_.getNextItemToPlay();
    this.log_("playing next track: " + track.getId());
    if (this.playingTrackInfo_!= undefined){
        this.playingTrackInfo_.resetEmpty();
    }
    this.playlist_.playItem(track);
}

kramerius.audio.Controller.prototype.canPlayPreviousTrack = function(){
    return this.playlist_.getPreviousItemToPlay() != null;
}

kramerius.audio.Controller.prototype.playPreviousTrack = function(){
    var track = this.playlist_.getPreviousItemToPlay();
    this.log_("playing previous track: " + track.getId());
    if (this.playingTrackInfo_!= undefined){
        this.playingTrackInfo_.resetEmpty();
    }
    this.playlist_.playItem(track);
}


kramerius.audio.Controller.prototype.setControllerState = function(state){
    this.log_("setting controller state to " + state);
    switch(state){
        case 'empty':
            this.setPosition(0);
            this.playPauseButton_.setStatePlayEnabled();
            //this.stopButton_.setDisabledImage();
            this.progressBar_.disable();
            if (this.rotatingRecordAnimation_!= undefined){
                this.rotatingRecordAnimation_.stopRotating();
            }
            if (this.playingTrackInfo_!= undefined){
                this.playingTrackInfo_.resetEmpty();
            }
            this.refreshPlayNextPlayPreviousButtons();
            break;
        case 'stopped':
            this.playPauseButton_.setStatePlayEnabled();
            //this.stopButton_.setDisabledImage();
            this.progressBar_.disable();
            this.setPosition(0);
            if (this.rotatingRecordAnimation_!= undefined){
                this.rotatingRecordAnimation_.stopRotating();
            }
            if (this.playingTrackInfo_!= undefined){
                this.playingTrackInfo_.resetText();
            }
            break;
        case 'playing':
            this.playPauseButton_.setStayPauseEnabled();
            //this.stopButton_.setEnabledImage();
            this.progressBar_.enable();
            if (this.rotatingRecordAnimation_!= undefined){
                this.rotatingRecordAnimation_.startRotating();
            }
            if (this.playingTrackInfo_!= undefined){
                this.playingTrackInfo_.animate();
            }
            break;
        case 'paused':
            this.playPauseButton_.setStatePlayEnabled();
            //this.stopButton_.setEnabledImage();
            //this.progressBar_.enable();
            if (this.rotatingRecordAnimation_!= undefined){
                this.rotatingRecordAnimation_.stopRotating();
            }
            if (this.playingTrackInfo_!= undefined){
                this.playingTrackInfo_.resetText();
                this.playingTrackInfo_.pauseAnimation();
            }
            break;
    }
};

kramerius.audio.Controller.prototype.play = function(){
    this.log_("play");
    this.audioPlayer_.play();
};

kramerius.audio.Controller.prototype.pause = function(){
    this.log_("pause");
    //    if (!this.audioPlayer_.isPlayingOrBuffering()){
    //        this.log_("WARNING: cannot pause: not playing");    
    //    }
    this.audioPlayer_.pause();
}

kramerius.audio.Controller.prototype.resume = function(){
    this.log_("resume");
    this.audioPlayer_.resume();
}


kramerius.audio.Controller.prototype.stop = function(){
    this.log_("stop");
    this.audioPlayer_.stop();
}

kramerius.audio.Controller.prototype.clear = function(){
    this.log_("clearing");
    this.setTrack(null);
}


kramerius.audio.Controller.prototype.setVolume = function(volume){
    if (volume < MIN_VOLUME || volume > MAX_VOLUME){
        this.log_("WARNING: Illegal volume level " + volume + ". Ignoring.")
    }else{
        //console.log("Controller: setting volume level to " + volume)
        this.volume_ = volume;
        if (this.volumeInfo_!= undefined){
            this.volumeInfo_.setVolume(this.volume_);
        }
        //set volume of playling track
        if (this.audioPlayer_ != undefined){
            this.audioPlayer_.setVolume(volume);
        }
        
        this.log_("volume level=" + volume);    
        if (this.mute_ == true){
            this.unMute();
        }
    }
}

kramerius.audio.Controller.prototype.mute = function(){
    this.log_("Controller: muting");
    //TODO: predelat
    //poslat audio playeru, volume slideru hodnotu 0
    //uchovat ale puvodni hlasitost
    
    //volume slider zase zpet nastavi hlasitost v controlloru 
    //na 0, proto ji vracim na puvodni hodnotu
    //a az potom zmenim ikonu mute/unmute tlacitka
    //interni hodnotu mute a hlasitost audio playeru
    var volumeBeforeMuting = this.volume_;
    this.volumeSlider_.setPosition(0);
    this.volume_ = volumeBeforeMuting;
    this.volumeInfo_.setVolume(0);
    this.volumeButton_.setStateMuted();
    this.mute_ = true;
    this.audioPlayer_.setVolume(0);
    
//this.volumeSlider_.disable();
}

kramerius.audio.Controller.prototype.unMute = function(){
    this.log_("unmuting, volume level: " + this.volume_);
    this.mute_ = false;
    this.audioPlayer_.setVolume(this.volume_);
    this.volumeInfo_.setVolume(this.volume_);
    this.volumeButton_.setStateUnmuted();
    this.volumeSlider_.setPosition(this.volume_);
//this.volumeSlider_.enable();
}

kramerius.audio.Controller.prototype.setPlayPauseButton = function(button){
    this.playPauseButton_ = button;
}

kramerius.audio.Controller.prototype.setStopButton = function(button){
    this.stopButton_ = button;
}

kramerius.audio.Controller.prototype.setPlayNextButton = function(button){
    this.playNextButton_ = button;
}

kramerius.audio.Controller.prototype.setPlayPreviousButton = function(button){
    this.playPreviousButton_ = button;
}

kramerius.audio.Controller.prototype.setVolumeButton = function(button){
    this.volumeButton_ = button;
}

kramerius.audio.Controller.prototype.setProgressBar = function(progressBar){
    this.progressBar_ = progressBar;
}

kramerius.audio.Controller.prototype.setProgressInfo = function(progressInfo){
    this.progressInfo_ = progressInfo;
}


kramerius.audio.Controller.prototype.setVolumeSlider = function(volumeSlider){
    this.volumeSlider_ = volumeSlider;
    //ani tady se nechyta 
    //a nenastavuje ze startu volumeSlider na spravnou hodnotu
    //this.volumeSlider_.setPosition(this.getActualVolume());
    this.volumeSlider_.setPosition(this.volume_);
}

kramerius.audio.Controller.prototype.setVolumeInfo= function(volumeInfo){
    this.volumeInfo_ = volumeInfo;
}


kramerius.audio.Controller.prototype.setPlaylistShowHideButton = function(playlistShowHideButton){
    this.playlistShowHideButton_ = playlistShowHideButton;
}

kramerius.audio.Controller.prototype.setShufflePlaylistButton = function(button){
    this.shufflePlaylistButton_ = button;
}

kramerius.audio.Controller.prototype.setPlaylist = function(playlist){
    if (playlist == undefined){
        this.log_("setPlaylist: playlist undefined");
    }
    this.playlist_ = playlist;
}

kramerius.audio.Controller.prototype.setRotatingRecord = function(rotatingRecordAnimation){
    this.rotatingRecordAnimation_ = rotatingRecordAnimation;   
}

kramerius.audio.Controller.prototype.setPlayingTrackInfo = function(playingTrackInfo){
    this.playingTrackInfo_ = playingTrackInfo;
}

kramerius.audio.Controller.prototype.getActualVolume = function(){
    if (this.mute_){
        this.log_("actual volume = 0");
        return 0;
    }else{
        this.log_("actual volume = " + this.volume_);
        return this.volume_;
    }
}

kramerius.audio.Controller.prototype.hidePlaylist = function(){
    this.playlist_.hide();
    this.playlistShowHideButton_.setStatePlaylistOff();
}

kramerius.audio.Controller.prototype.showPlaylist = function(){
    this.playlist_.show();
    this.playlistShowHideButton_.setStatePlaylistOn();
}

kramerius.audio.Controller.prototype.log_ = function(text){
    if (this.loggingEnabled_){
        console.log("Controller: " + text);
    }
}

kramerius.audio.Controller.prototype.activateSelectedTrackIfNoActive = function(){
   if (this.playlist_.getActiveItem() == undefined){
       this.playlist_.getSelectedItem().setStatusActive();
   }
}





